// const orderObj = uniCloud.importObject('mall-order');
const db = uniCloud.database();
const cmd = db.command;
module.exports = async (obj) => {
    let {
        data = {}
    } = obj;
    let user_order_success = true;
    // uni-pay-orders 表内的数据均可获取到
    let {
        order_no,
        total_fee
    } = data;
    //全部商品
    let orderRes = await db.collection('mall-order').where({
        order_no: order_no
    }).get();
    let cardList = orderRes.data[0].cardList;
    //订单编号
    let orderIdx = cardList.map(item => item._id);
    //订单内包含的商品
    let goodsRes = await db.collection('mall-goods').where({
        _id: cmd.in(orderIdx)
    }).get();
    //比较价格
    let sum = 0;
    goodsRes.data.map(item => {
        let index = cardList.findIndex(goods => goods._id == item._id);
        sum += cardList[index].shopNum * item.money
    })
    if (sum == total_fee) {
        db.collection('mall-order').where({
            order_no
        }).update({
            states: 1
        })

    } else {
        db.collection('mall-order').where({
            order_no
        }).update({
            states: -1,
            sum
        })
        user_order_success = false;
    };
    return user_order_success;
    // user_order_success = await orderObj.getCountMoney(order_no, total_fee);
};